import sys
from datetime import datetime
from PySide6 import QtCore
from PySide6.QtGui import QColor, QIcon
from PySide6.QtWidgets import QTableView, QMainWindow, QApplication
from PySide6.QtCore import Qt

COLORS = ['#053061', '#2166ac', '#4393c3', '#92c5de', '#d1e5f0',
'#f7f7f7', '#fddbc7', '#f4a582', '#d6604d', '#b2182b', '#67001f']

class TabelModel(QtCore.QAbstractTableModel):
    def __init__(self, data):
        super().__init__()
        self._data = data

    def data(self, index, role):
        # cell 显示内容
        if role == Qt.DisplayRole:
            value = self._data[index.row()][index.column()]

            if isinstance(value, datetime):
                return value.strftime("%Y-%m-%d")

            if isinstance(value, float):
                return "%.2f" % value
            
            if isinstance(value, str):
                return '"%s"' % value
            
            return value
        
        # cell 背景颜色
        # if role == Qt.BackgroundRole:
        #     value = self._data[index.row()][index.column()]
        #     if isinstance(value, int) or isinstance(value, float):
        #         value = int(value)

        #         value = max(-5, value)
        #         value = min(5, value)
        #         value = value + 5

        #         return QColor(COLORS[value])
        
        # cell 文本对齐方向
        if role == Qt.TextAlignmentRole:
            value = self._data[index.row()][index.column()]

            if isinstance(value, int) or isinstance(value, float):
                return Qt.AlignVCenter | Qt.AlignRight
            
        # cell 内容颜色
        if role == Qt.ForegroundRole:
            value = self._data[index.row()][index.column()]

            if isinstance(value, int) or isinstance(value, float):
                return QColor(Qt.red)
            
        # cell 装饰
        if role == Qt.DecorationRole:
            value = self._data[index.row()][index.column()]
            if isinstance(value, datetime):
                return QIcon('calendar.png')
            
            if isinstance(value, bool):
                if value:
                    return QIcon('tick.png')
                
                return QIcon('cross.png')
            
            if isinstance(value, int) or isinstance(value, float):
                value = int(value)

                value = max(-5, value)
                value = min(5, value)
                value = value + 5

                return QColor(COLORS[value])
        
    def rowCount(self, index):
        return len(self._data)
    
    def columnCount(self, index):
        return len(self._data[0])
    
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.table = QTableView()

        data = [
            [True, 9, 2],
            [1, -1, 'hello'],
            [3.024, 5, -5],
            [3, 3, datetime(2025, 12, 23)],
            [7.555, False, 0]
        ]
        self.model = TabelModel(data)
        self.table.setModel(self.model)

        self.setCentralWidget(self.table)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()

